<?php
namespace app\service;
use alipay\AopClient;
use alipay\AlipayEcoMycarParkingConfigSetRequest;//停车ISV系统配置接口
use alipay\AlipayEcoMycarParkingConfigQueryRequest;//ISV系统配置查询接口
use alipay\AlipayEcoMycarParkingParkinglotinfoCreateRequest;//录入停车场信息
use alipay\AlipayEcoMycarParkingParkinglotinfoUpdateRequest;//修改停车场信息
use alipay\AlipayEcoMycarParkingEnterinfoSyncRequest;//车辆驶入接口
use alipay\AlipayEcoMycarParkingExitinfoSyncRequest;//车辆驶出接口
use alipay\AlipayEcoMycarParkingVehicleQueryRequest;//车牌查询接口
use alipay\AlipayEcoMycarParkingOrderSyncRequest;//订单同步接口
use alipay\AlipayEcoMycarParkingOrderUpdateRequest;//订单更新接口
use alipay\AlipaySystemOauthTokenRequest;//换取授权访问令牌
use alipay\AlipayTradeCreateRequest;//生成订单
use alipay\AlipayTradeCancelRequest;//删除账单
use alipay\AlipayEcoMycarParkingAgreementQueryRequest;//车牌代扣状态查询
use alipay\AlipayEcoMycarTradeOrderQueryRequest;//车牌代扣状态查询
use alipay\AlipayEcoMycarParkingOrderPayRequest;//停车缴费代扣接口
use alipay\AlipayEcoMycarParkingOrderRefundRequest;//停车代扣退款接口
use alipay\AlipayMarketingCashlessvoucherTemplateCreateRequest;//优惠券模板创建借口
use alipay\AlipayMarketingVoucherSendRequest;//优惠券模板发送接口
use alipay\AlipayMarketingVoucherTemplatedetailQueryRequest;//优惠券模板详情查询接口

use alipay\AlipayEbppInvoiceTitleListGetRequest;

use alipay\AlipayEbppInvoiceInfoSendRequest;
use stdClass;

class apiService{
    const PRIKEYPATH = '/www/wwwroot/zhwy.aliwgzf.com/public/rsa/prikey.txt';//私钥路径
    const PUBKEYPATH = '/www/wwwroot/zhwy.aliwgzf.com/public/rsa/alipay_pubkey.txt';//支付宝公钥路径

    private $app_id;//支付宝分配给开发者的应用
    private $method;//接口名称
    private $format;//仅支持JSON  no
    private $charset = 'UTF-8';//请求使用的编码格式，如utf-8,gbk,gb2312等
    private $sign_type = 'RSA2';//商户生成签名字符串所使用的签名算法类型，目前支持RSA2和RSA，推荐使用RSA2
    private $sign;//商户请求参数的签名串  no
    private $timestamp;//发送请求的时间，格式"yyyy-MM-dd HH:mm:ss"
    private $version = '1.0';//调用的接口版本，固定为：1.0
    private $app_auth_token;//详见应用授权概述 no
    private $biz_content;//请求参数的集合，最大长度不限，除公共参数外所有请求参数都必须放在这个参数中传递，具体参照各产品快速接入文档

    //第三方应用
    public function getPublicContent(){
        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = env("APP.ALIPAY_APPID");
        $aop->rsaPrivateKey = file_get_contents(self::PRIKEYPATH);//私钥路径
        $aop->alipayrsaPublicKey= file_get_contents(self::PUBKEYPATH);//支付宝公钥路径
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        return $aop;
    }

    public function invoicePublicContent($auth_token){
        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '2017100709184374';
        $aop->rsaPrivateKey = file_get_contents(self::PRIKEYPATH);//私钥路径
        $aop->alipayrsaPublicKey= file_get_contents(self::PUBKEYPATH);//支付宝公钥路径
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        $aop->auth_token=$auth_token;
        return $aop;
    }

    //生活号应用
    public function lifegetPublicContent(){
        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '2019041964002011';
        $aop->rsaPrivateKey = file_get_contents(self::PRIKEYPATH);//私钥路径
        $aop->alipayrsaPublicKey= file_get_contents(self::PUBKEYPATH);//支付宝公钥路径
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        return $aop;
    }

    //换取授权访问令牌
    public function oauthToken($code){
        $aop = $this->getPublicContent();
        $request = new AlipaySystemOauthTokenRequest ();
        $request->setGrantType("authorization_code");
        $request->setCode($code);
        $request->setRefreshToken('');
        $result = $aop->execute ( $request);
        if(property_exists($result,'error_response')){
            $responseNode = 'error_response';
        }else{
            $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";   
        }
        return $result->$responseNode;
    }
    //创建订单
    public function create($data){
        $data = json_decode($data);
        $aop = $this->getPublicContent(); 
        $request = new AlipayTradeCreateRequest ();
        $request->setBizContent("{" .
        "\"out_trade_no\":\"$data->out_trade_no\"," .
        "\"total_amount\":$data->total_amount," .
        "\"subject\":\"$data->subject\"," .
        "\"buyer_id\":\"$data->buyer_id\"," .
        "\"extend_params\":{".
            "\"sys_service_provider_id\":\"2088621722997814\"".
            "},".
        "\"timeout_express\":\"15d\"" .
        "  }");
        $request->setNotifyUrl($data->notifyurl);
        
        $token = $data->token;
        $result = $aop->execute ($request, null, $token);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }
    

	public  function orderCreate($data){
		//    2088521066336121
		$sys_service_provider_id="2088031574378734";
		$data = json_decode($data);
        $aop = $this->getPublicContent(); 
        $request = new AlipayTradeCreateRequest ();
        $request->setBizContent("{" .
        "\"out_trade_no\":\"$data->out_trade_no\"," .
        "\"total_amount\":$data->total_amount," .
        "\"subject\":\"$data->subject\"," .
        "\"buyer_id\":\"$data->buyer_id\"," .
        "\"promo_params\":{".
            "\"parking_id\":\"$data->parking_lot_id\"".
            "},".
        "\"extend_params\":{" .
            "\"sys_service_provider_id\":\"$sys_service_provider_id\"," .
            "\"industry_reflux_info\":\"{\\\"scene_code\\\":\\\"parking_fee_order\\\",\\\"channel\\\":\\\"common_park_provider\\\",\\\"scene_data\\\":{\\\"license_plate\\\":\\\"$data->car_number\\\",\\\"start_time\\\":\\\"$data->start_time\\\",\\\"parking_lot_id\\\":\\\"$data->parking_lot_id\\\",\\\"parkling_lot_name\\\":\\\"$data->parkling_lot_name\\\",\\\"city_code\\\":\\\"$data->city_code\\\"}}\"" .
            "    },".
        "\"invoice_info\":{" . 
            "\"key_info\":{" .
				"\"is_support_invoice\":\"$data->is_support_invoice\",".
				"\"invoice_merchant_name\":\"$data->invoice_merchant_name\",".
				"\"tax_num\":\"$data->tax_num\"".
				"}" .
        "    },".
        "\"timeout_express\":\"5m\"" .
        "  }");
        $request->setNotifyUrl($data->notifyurl);
        $token = $data->token;
        $result = $aop->execute ($request, null, $token);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }

    public  function createinvoice($data){
		$sys_service_provider_id="2088621722997814";//    2088521066336121
		$data = json_decode($data);
        $aop = $this->getPublicContent(); 
        $request = new AlipayTradeCreateRequest ();
        $request->setBizContent("{" .
        "\"out_trade_no\":\"$data->out_trade_no\"," .
        "\"total_amount\":$data->total_amount," .
        "\"subject\":\"$data->subject\"," .
        "\"buyer_id\":\"$data->buyer_id\"," .
        "\"promo_params\":{".
            "\"parking_id\":\"$data->parking_lot_id\"".
            "},".
        "\"extend_params\":{" .
            "\"sys_service_provider_id\":\"$sys_service_provider_id\"," .
            "\"industry_reflux_info\":\"{\\\"scene_code\\\":\\\"parking_fee_order\\\",\\\"channel\\\":\\\"common_park_provider\\\",\\\"scene_data\\\":{\\\"license_plate\\\":\\\"$data->car_number\\\",\\\"start_time\\\":\\\"$data->start_time\\\",\\\"parking_lot_id\\\":\\\"$data->parking_lot_id\\\",\\\"parkling_lot_name\\\":\\\"$data->parkling_lot_name\\\",\\\"city_code\\\":\\\"$data->city_code\\\"}}\"" .
            "    },".
		"\"invoice_info\":{" . 
            "\"key_info\":{" .
				"\"is_support_invoice\":\"$data->is_support_invoice\",".
				"\"invoice_merchant_name\":\"$data->invoice_merchant_name\",".
				"\"tax_num\":\"$data->tax_num\"".
				"}" .
        "    },".
        "\"timeout_express\":\"5m\"" .
        "  }");
        $request->setNotifyUrl($data->notifyurl);
        $token = $data->token;
        $result = $aop->execute ($request, null, $token);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }
    public  function tradeCreate($data){
        $sys_service_provider_id="2088621722997814";//    2088521066336121
        $data = json_decode($data);
        $aop = $this->getPublicContent();
        $request = new AlipayTradeCreateRequest ();
        $object = new stdClass();
        $object->out_trade_no = $data->out_trade_no;
        $object->total_amount = $data->total_amount;
        $object->subject = $data->subject;
        $object->buyer_id = $data->buyer_id;
        //$object->sell_id=$data->sell_id;
        //$object->product_code = 'JSAPI_PAY';//   JSAPI_PAY FACE_TO_FACE_PAYMENT
        $json = json_encode($object);

        //Log::write($json);
        $request->setBizContent($json);
        $request->setNotifyUrl($data->notifyurl);
        $token = $data->token;
        $result = $aop->execute ($request, null, $token);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }
    
    public  function gatepayCreate($data){
		$sys_service_provider_id="2088621722997814";//    2088521066336121
		$data = json_decode($data);
        $aop = $this->getPublicContent(); 
        $request = new AlipayTradeCreateRequest ();
        $object = new stdClass();
        $object->out_trade_no = $data->out_trade_no;
        $object->total_amount = $data->total_amount;
        $object->subject = $data->subject;
        $object->buyer_id = $data->buyer_id;
        //$object->sell_id=$data->sell_id;
        //$object->product_code = 'JSAPI_PAY';//   JSAPI_PAY FACE_TO_FACE_PAYMENT
        $json = json_encode($object);

        //Log::write($json);
        $request->setBizContent($json);
        $request->setNotifyUrl($data->notifyurl);
        $token = $data->token;
        $result = $aop->execute ($request, null, $token);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }

	
	
    
}